<?php
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "NeoSession.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "NeoFunctionSession.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "DataPlayer.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "FunctionArrayData.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "FunctionGameLoto.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "ArrayRandomNumber.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  2  ).'/' .'/'.  "Card.php"  )  ;
class svc_ServerLoTo {
    private $args = null;
    function __construct($arg) {
        $this->args = $arg;
    }
    function getPublish() {
        $appid = 2;
        $lang = $this->args["lang"];
        if ($lang == null || $lang == "")
            $lang = "html";
        $request = $this->args["request"];
        $rs = new Result();
        $rs->addData("request", $request);
        if ($lang == 'bin') {
            $rcode = $this->args['rcode'];
            if ($rcode == null || $rcode == '')
                $rcode = 0;
            $rs->addData("rcode", 0 + $rcode);
        }
        if ($request == "getPage") {
            $rs->addData("content", "Hello LoTo");
        }
        else if($request == "signupNeo")
        {
            $playername = $_REQUEST["playername"];
            $mailaddress = $_REQUEST["email"];
            $password = $_REQUEST["password"];
            if(strlen($mailaddress) > 0){
                $srs = FunctionGameLoto::registerAccount($mailaddress, $password,$playername);
                $rs->merge($srs);
                if ($srs->return) {
                    $srs = FunctionGameLoto::confirmRegistration($mailaddress, $rs->data["code"]);
                    if ($srs->return) {
                        $rs->return = true;
                        if ($lang != "bin") 
                        {
                            $rs->addData("content", callService("mainpage", null));
                        } else {
                        }
                    }
                }
            }
        }
        else if($request == "loginNeo")
        {
            $fp= fopen("text.txt",a) or exit("Khong tim thay file can mo");
            fwrite($fp, " User login \n");
            $mailaddress = $this->args["email_address"];
            $password = $this->args["password"];
            if(strlen($mailaddress) > 0){
                $rss = NeoFunctionSession::login($mailaddress,$password);
                $rs->merge($rss);
                if(NeoSession::isLogin()){
                        $rs->return = true;
                        if($lang != "bin"){
                                $rs->addData("userid",NeoSession::getID());
                        }else{
                                $reg_data = FunctionGameLoto::readAccountRegisterInfo($mailaddress);
                                for ($i = 2; $i < count($reg_data->GemOfPlayer) + 2; $i++) {
                                    fwrite($fp, $reg_data->GemOfPlayer[$i]. "\n");
                                }
                                for ($i = 0; $i < count($reg_data->PowOfPlayer); $i++) {
                                    fwrite($fp, $reg_data->PowOfPlayer[$i]."\n");
                                }
                                $rs->addData("content",implode(array_map("chr",   $reg_data->getBinary()  )) );
                        }
                }
            }
            fclose($fp);
        }
        else if($request == "updateStateCard")
        {
            $mailaddress = $_REQUEST["email"];
            $zoneID = $_REQUEST["zoneId"];
             $key_Of_card_arrNumber ="zoneID_".$zoneID."_mail_".$mailaddress."_card_arrNumber";
            $key_Of_card_arrStatusNumber = "zoneID_".$zoneID."_mail_".$mailaddress."_card_arrStatusNumber";
            $key_Of_card_arrTypePowerUp ="zoneID_".$zoneID."_mail_".$mailaddress."_card_arrTypePowerUp";
            $key_Of_card_arrExp = "zoneID_".$zoneID."_mail_".$mailaddress."_card_arrExp";
            $rscard_arrNumber = FunctionArrayData::get2ArrayFromDB($key_Of_card_arrNumber);
            $rscard_arrStatusNumber = FunctionArrayData::get2ArrayFromDB($key_Of_card_arrStatusNumber);
            $rscard_arrTypePowerUp = FunctionArrayData::get2ArrayFromDB($key_Of_card_arrTypePowerUp);
            $rscard_arrExp = FunctionArrayData::get2ArrayFromDB($key_Of_card_arrExp);
            $row = $this->args["row"];
            $column = $this->args["column"];
            $status = $this->args["status"];
            $powtype = $this->args["powtype"];
            $exp = $this->args["exp"];
            $fp= fopen("text.txt",a) or exit("Khong tim thay file can mo");
            fwrite($fp, "row is  $row \n");
            fwrite($fp, "column is  $column \n");
            fwrite($fp, "status is  $status \n");
            $rscard_arrStatusNumber[$row][$column] =(int) $status;
            if($powtype != NULL)
            {
                $rscard_arrTypePowerUp[$row][$column] =(int)$powtype;
                fwrite($fp, "powtype is  $powtype \n");
            }
            if($exp != NULL)
            {
                $rscard_arrExp[$row][$column] = (int)$exp;
                fwrite($fp, "exp is  $exp \n");
            }
            FunctionArrayData::write2ArrayData($key_Of_card_arrNumber, $rscard_arrNumber);
            FunctionArrayData::write2ArrayData($key_Of_card_arrStatusNumber, $rscard_arrStatusNumber);
            FunctionArrayData::write2ArrayData($key_Of_card_arrTypePowerUp, $rscard_arrTypePowerUp);
            FunctionArrayData::write2ArrayData($key_Of_card_arrExp, $rscard_arrExp);
            fclose($fp);
        }
        else if($request == "buycard")
        {
            $chip = $_REQUEST["chip"];
            $exp = $_REQUEST["exp"];
            $mailaddress = $_REQUEST["email"];
            $zoneID = $_REQUEST["zoneId"];
            $reg_data = FunctionGameLoto::readAccountRegisterInfo($mailaddress);
            $reg_data->chip= $chip;
            {if(!isset( FunctionData::$static  ) ||  FunctionData::$static  ==null){ FunctionData::$static   = new Memcache; FunctionData::$static  ->connect("127.0.0.1" , 11215 ) or die ("Could not connect");};} ;
            {if( FunctionData::$static  ->get( "mail_reginfo_" .$mailaddress )==null) FunctionData::$static  ->set( "mail_reginfo_" .$mailaddress , $reg_data->getBinary() );else  FunctionData::$static  ->replace( "mail_reginfo_" .$mailaddress , $reg_data->getBinary() );} ;
            $card = new Card($exp);
             FunctionGameLoto::registerCard($zoneID,$mailaddress, $card);
            $rcard = FunctionGameLoto::readCardInfo($zoneID, $mailaddress);
            $size = 4* 4* 6 *9;
            $buf = ByteBuffer::allocate( $size ) ;
            $fp= fopen("text.txt",a) or exit("Khong tim thay file can mo");
            fwrite($fp, "Buy Card  \n");
            for ( $iRow = 0; $iRow < 9; $iRow++)
            {
                for ($iColumn = 0; $iColumn < 6; $iColumn++ )
                {
                    DataUtil::writeInt32( $buf ,  $rcard->arrNumber[$iRow][$iColumn] ) ;
                    DataUtil::writeInt32( $buf ,  $rcard->arrStatusNumber[$iRow][$iColumn] ) ;
                    DataUtil::writeInt32( $buf ,  $rcard->arrTypePowerUp[$iRow][$iColumn] ) ;
                    DataUtil::writeInt32( $buf ,  $rcard->exp[$iRow][$iColumn] ) ;
                }
                fwrite($fp,"\n");
            }
	    fclose($fp);
            $rs->return = true;
            $rs->addData("content", implode(array_map("chr",   $buf ->_array()  )) );
        }else if($request == "updateNum")
        {
            $zoneID = $_REQUEST["zoneId"]; 
            $key_Of_arrRanNumber ="zoneId_".$zoneID."_arrRanNumber";
            $key_Of_arrStatusNumber = "zoneId_".$zoneID."_arrStatusNumber";
            $rsNumberBin = FunctionArrayData::getArrayFromDB($key_Of_arrRanNumber);
            $rsStatusBin = FunctionArrayData::getArrayFromDB($key_Of_arrStatusNumber);
            if (isset($rsNumberBin)) {
                $size = 4 * 2;
                $buf = ByteBuffer::allocate( $size ) ;
                $fp = fopen("text.txt", a) or exit("Khong tim thay file can mo");
                $temp = 0;
                for ($i = 0; $i < 60; $i++) {
                    if ($rsStatusBin[$i] == 0) {
                        $temp = $i;
                        break;
                    }
                }
                $rsStatusBin[$temp] = 1;
                DataUtil::writeInt32( $buf ,  $temp ) ;
                DataUtil::writeInt32( $buf ,  $rsStatusBin[$temp] ) ;
                FunctionArrayData::writeArrayData($key_Of_arrStatusNumber,$rsStatusBin);
                fclose($fp);
            }
            $rs->return = true;
            $rs->addData("content", implode(array_map("chr",   $buf ->_array()  )) );
        }
        else if ($request == "getnumber") {
            $zoneID = $_REQUEST["zoneId"]; 
            $key_Of_arrRanNumber ="zoneId_".$zoneID."_arrRanNumber";
            $key_Of_arrStatusNumber = "zoneId_".$zoneID."_arrStatusNumber";
            $obj = new ArrayRandomNumber();
            FunctionArrayData::writeArrayData($key_Of_arrRanNumber, $obj->getArrayNumber());
            FunctionArrayData::writeArrayData($key_Of_arrStatusNumber, $obj->getArrayStatus());
            $rsNumberBin = FunctionArrayData::getArrayFromDB($key_Of_arrRanNumber);
            $rsStatusBin = FunctionArrayData::getArrayFromDB($key_Of_arrStatusNumber);
            $size = 60 * 4 * 2;
            $buf = ByteBuffer::allocate( $size ) ;
            $fp = fopen("text.txt", a) or exit("Khong tim thay file can mo");
            fwrite($fp, "get number:\n");
            for ($i = 0; $i < 60; $i++) {
                DataUtil::writeInt32( $buf ,  $rsNumberBin[$i] ) ;
                DataUtil::writeInt32( $buf ,  $rsStatusBin[$i] ) ;
                fwrite($fp, "n.$rsNumberBin[$i]");
                fwrite($fp, "s.$rsStatusBin[$i]");
            }
            fwrite($fp, "\nEnd!\n");
            fclose($fp);
            $rs->return = true;
            $rs->addData("content", implode(array_map("chr",   $buf ->_array()  )) );
        } else {
            {Error::getInstance() ->addError( -1 ,  "Unknows Request" );} ;
        }
        return  $rs ->publish(  $lang ) ;
    }
}
?>